package com.itheima.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.itheima.dao.ProductDao;
import com.itheima.domain.Category;
import com.itheima.domain.Order;
import com.itheima.domain.PageBean;
import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;

public class ProductService {

	public List<Product> findHotProduct() {
		ProductDao productDao  = new ProductDao();
	
		return productDao.findHotProduct();
	
	}

	public List<Product> findLastProduct() {
		ProductDao productDao  = new ProductDao();
		return productDao.findLastProduct();
		
	}

	public List<Category> findAllCategory() {
		ProductDao productDao  = new ProductDao();
		return productDao.findAllCategory();
	}

	public  PageBean<Product> productById(String cid,int currentPage,int currentCount) {
		// TODO Auto-generated method stub
		PageBean<Product> pageBean= new PageBean<Product>();
		pageBean.setCurrentCount(currentCount);
		pageBean.setCurrentPage(currentPage);
		ProductDao productDao  = new ProductDao();
		int count =productDao.getCount(cid);
		pageBean.setTotalPage((int) Math.ceil(1.0*count/currentCount));
		pageBean.setTotalCount(count);
		//当前索引
		int index = (currentPage-1)*currentCount;
		List<Product> list = null;
		
		list = productDao.findProductByPage(cid,index,currentCount);
		pageBean.setList(list);
		return  pageBean;
	}

	public Product findProductById(String pid) {
		ProductDao productDao  = new ProductDao();
		 return productDao.findProductById(pid);
		// TODO Auto-generated method stub
		
	}

	public void submitOrder(Order order) {
			ProductDao dao = new ProductDao();
			//1、开启事务
			try {
				DataSourceUtils.startTransaction();
				//2、调用dao存储order表数据的方法
				dao.addOrders(order);
				//3、调用dao存储orderitem表数据的方法
				dao.addOrderItem(order);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				try {
					DataSourceUtils.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}finally {
				try {
					DataSourceUtils.commitAndRelease();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		
		
	}

	public void updateOrderAdrr(Order order) {
		ProductDao dao = new ProductDao();
		try {
			dao.updateOrderAdrr(order);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateOrderState(String r6_Order) {
		ProductDao dao = new ProductDao();
		try {
			dao.updateOrderState(r6_Order);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

		//根据关键字查询商品
	public List<Object> findProductByWord(String word) throws SQLException {
		ProductDao dao = new ProductDao();
		return dao.findProductByWord(word);
	}

	//获得指定用户的订单集合
	public List<Order> findAllOrders(String uid) {
		ProductDao dao = new ProductDao();
		List<Order> orderList = null;
		try {
			orderList = dao.findAllOrders(uid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orderList;
	}

	public List<Map<String, Object>> findAllOrderItemByOid(String oid) {
		ProductDao dao = new ProductDao();
		List<Map<String, Object>> mapList = null;
		try {
			mapList = dao.findAllOrderItemByOid(oid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mapList;
	}


}
